import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_business_core/business_customer/models/business_customer_details_dynamic_model.dart';

class BusinessCustomerDetailsDynamicInteractContent extends StatelessWidget {

  final int interactType;

  final String interactContent;

  const BusinessCustomerDetailsDynamicInteractContent({Key key, @required this.interactContent, @required this.interactType})
      : assert(interactContent != null),
        assert(interactType != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget child;
    try {
      Map data = jsonDecode(interactContent);
      BusinessCustomerDetailsDynamicModel _model = _matchTemplate(interactType: interactType, item: data);
      child = Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(_model.title ?? ''),
          SizedBox(height: 8),
          if (_model.img != null && _model.img.isNotEmpty)
            Container(constraints: BoxConstraints(maxHeight: 180), child: BaseImageComponent(imageUrl: _model.img, hero: '${_model.img}${new Random().nextInt(1000)}', fit: BoxFit.cover)),
        ],
      );
    } catch (e) {
      child = Text(interactContent);
    }
    return Container(child: child);
  }

  BusinessCustomerDetailsDynamicModel _matchTemplate({@required int interactType, @required Map<String, dynamic> item}) {
    BusinessCustomerDetailsDynamicModel _model;
    switch (interactType) {
      case 1052: //心意卡
        _model = BusinessCustomerDetailsDynamicModel(id: item['cardId'], title: item['cardName'], img: item['facadeImage']);
        break;
      case 1051: // 话题
        _model = BusinessCustomerDetailsDynamicModel(id: item['topicId'], title: item['topicTitle'], img: item['topicImg']);
        break;
    }
    return _model;
  }

}
